﻿using System;
using System.Threading.Tasks;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using Fetches.Fetch.Dto;
using Fetches.Fetch.Factory;
using Fetches.Fetch.Fetcher;

namespace Fetches.Custom.Legacy
{
    /// <summary>
    /// Fetches的分布式针对的是Fetcher，SiteOrder由于有Processor所以无法分布式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class SiteOrder<T> : FetchOrder<T> where T : AbstractFetcher
    {
        public abstract Action<ResponsePage> DefaultProcessor();
        /// <summary>
        /// 要支持SiteOrder的分布式重点在于Processor的分布式调度
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public FetchReceipt FetchAndProcess(IFetchFactory factory)
        {
            var receipt = factory.Fetch(this);
            var consumer = factory.Consume(receipt);
            consumer.AddPageProcessor(DefaultProcessor());
            consumer.StartConsume();
            return receipt;
        }

        protected IHtmlDocument ParseHtml(string source)
        {
            var html = new HtmlParser().ParseDocument(source);
            return html;
        }
    }
}
